import { observable, action } from 'mobx'
import {axiosFetch, handleResp} from "../../util/common/http";
import history from "../../util/router/history";

export class DishDetailStore {
	@observable dish: {
		dishId: number,
		season: number|null,
		oilyDegree: number,
		dishType: number,
		dishName: string|null,
		dishPic: string|null,
		dishDesc: string|null,
		dishSource: string|null,
		hotGrade: number,
	}|null = null;

	// 菜品状态(1:可查看 0:菜品不存在or出错了)
	@observable dishStatus: number = 1;

	@action initDishInfo = async (dishId: number) => {
		const dishInfoResp = await axiosFetch("/dish/query/one", {
			method: 'POST',
			data: {
				dishId: dishId
			}
		});
		if (handleResp(dishInfoResp, false)) {
			this.dish = dishInfoResp.context;
		} else {
			this.dishStatus = 0;
		}
	};

	@action confirmEat = async () => {
		if (!this.dish) {
			return;
		}

		const addResp = await axiosFetch("/dish/hotGrade/incr", {
			method: 'POST',
			data: {
				dishId: this.dish.dishId
			}
		});

		if (handleResp(addResp)) {
			this.dish.hotGrade++;
		}
	};

	@action confirmDelete = async () => {
		if (!this.dish) {
			return;
		}

		const delResp = await axiosFetch("/dish/delete", {
			method: 'DELETE',
			data: {
				dishId: this.dish.dishId
			}
		});

		if (handleResp(delResp)) {
			history.goBack();
		}
	};

}

export default new DishDetailStore();
